/*
<Copyright (C) 2011 Dru Moore>

This file is part of demorecorder.

vlc-remote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

demorecorder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with demorecorder.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "utilities.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
//#include <QStringBuilder>
#include <QDebug>;

Utilities::Utilities()
{
    // no op
}

bool Utilities::doesPathExist(const QString path) {
    return doesFileExist(path) || doesDirectoryExist(path);
}

bool Utilities::doesFileExist(const QString path) {
    return QFile(path).exists();
}

bool Utilities::doesDirectoryExist(const QString path) {
    return QDir(path).exists();
}

bool Utilities::createAllDirectories(const QString path) {
    return QDir().mkpath(path);
}

QString Utilities::incrementFileName(QString path) {
    // Format /path/file-N.xxx
    // Increments N
    //qDebug() << "In incrementFileName: " << path;
    QFileInfo info(path);
    QString dir = info.absolutePath() + "/";
    QString ext = info.completeSuffix();
    if (0 < ext.length()) ext = "." + ext;
    QString filename = info.baseName();
    int x = 0;
    QStringList sl = filename.split("-", QString::SkipEmptyParts);
    if (1 < sl.size()) {
        //qDebug() << "Have parts " << sl.size();
        filename = sl.at(0);
        QString num = sl.at(sl.size() - 1);
        //qDebug() << "Num " << num;
        //qDebug() << "Filename " << filename;
        x = num.toInt();
    }
    else {
        x = 0;
    }
    // should release the resource?
    //qDebug() << "x=" << x;
    ++x;
    return dir + filename + "-" + QString::number(x).rightJustified(3, '0', false) + ext;
}

QString Utilities::getNextAvailableFileName(QString path) {
    //qDebug() << "Path: " << path;
    QString testPath = path;
    while (doesFileExist(testPath)) {
        testPath = incrementFileName(testPath);
    }
    qDebug() << "Track Path:" << testPath;
    return testPath;
}

QString Utilities::getHomeDirectory() {
    return QDir::homePath();
}

QString Utilities::getDirectoryName(const QString path) {
    return QDir(path).dirName();
}

QString Utilities::getFileBaseName(const QString path) {
    return QFileInfo(path).baseName();
}

QString Utilities::getFileName(const QString path) {
    return QFileInfo(path).fileName();
}

void Utilities::setGObjectProperty(GObject* el, const gchar* name, bool value) {
    GValue val = {0, };
    g_value_init (&val, G_TYPE_BOOLEAN);
    g_value_set_boolean(&val, value);
    g_object_set_property(el, name, &val);
    //delete val;
}

void Utilities::setGObjectProperty(GObject* el, const gchar* name, long value) {
    GValue val = {0, };
    g_value_init (&val, G_TYPE_INT);
    g_value_set_int(&val, value);
    g_object_set_property(el, name, &val);
    //delete val;
}

void Utilities::setGObjectProperty(GObject* el, const gchar* name, long long value) {
    GValue val = {0, };
    g_value_init (&val, G_TYPE_INT64);
    g_value_set_int64(&val, value);
    g_object_set_property(el, name, &val);
    //delete val;
}

void Utilities::setGObjectProperty(GObject* el, const gchar* name, double value) {
    GValue val = {0, };
    g_value_init (&val, G_TYPE_DOUBLE);
    g_value_set_double(&val, value);
    g_object_set_property(el, name, &val);
    //delete val;
}

void Utilities::setGObjectProperty(GObject* el, const gchar* name, QString value) {
    GValue val = {0, };
    g_value_init (&val, G_TYPE_STRING);
    g_value_set_string(&val, value.toLatin1());
    g_object_set_property(el, name, &val);
    //delete val;
}
